/*
    商城配置:APP首页配置
 */
$(function () {
    $("#jqGrid").Grid({
        height: '670px',
        url: '../homeconfig/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '首页模块名称', name: 'modelName', index: 'model_name', width: 80},
            {label: '类型', name: 'type', index: 'type', width: 20},
            {label: '类型名称', name: 'typeName', index: 'type_name', width: 80},
            {label: '业务编号', name: 'bussnessid', index: 'bussnessid', width: 20},
            {label: '业务名称', name: 'bussnessName', index: 'bussness_name', width: 80},
            {
                label: '图片', name: 'imageUrl', index: 'image_url', width: 80, formatter: function (value) {
                    return transImg(value);
                }
            },
            {label: '排序', name: 'sort', index: 'sort', width: 20},
            {label: '标题', name: 'title', index: 'title', width: 20},
            {label: '描述', name: 'describtion', index: 'describtion', width: 20},
            {label: '创建时间', name: 'createDate', index: 'create_date', width: 80}]
    });
});
let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        homeConfig: {},
        cates: [],
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            type: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.homeConfig = {};
            Ajax.request({
                url: '../categoryconf/queryAll',
                type: "GET",
                contentType: "application/json",
                successCallback: function (r) {
                    vm.cates = r.list;
                }
            });
        },
        test: function () {
            console.log("=============")
            Ajax.request({
                url: 'localhost:8080/sport/api/invite/reg',
                params: JSON.stringify("{url:url地址}"),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    console.log("=============")
                }
            });
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.homeConfig.id == null ? "../homeconfig/save" : "../homeconfig/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.homeConfig),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../homeconfig/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../homeconfig/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.homeConfig = r.homeConfig;
                }
            });
            Ajax.request({
                url: '../categoryconf/queryAll',
                type: "GET",
                contentType: "application/json",
                successCallback: function (r) {
                    vm.cates = r.list;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name , 'type':vm.q.type},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                type: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.homeConfig.imageUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.homeConfig.imageUrl;
            eyeImage(url);
        },

        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },
        openSpe: function () {
            openWindow({
                type: 2,
                title: '业务选择',
                area: ['1000px', '600px'],
                //area: ['60%', '50%'],
                content: ['homeconfigselect.html'],
                btn: ['确定', '取消'],
                btn1: function (index) {
                    let ids = getSelectedRows("#jqGridSelect");
                    if (ids == null){
                        return;
                    }
                    vm.homeConfig.bussnessid = ids[0];
                   // layer.close(index);
                }

            })
        }

    }

});
